package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param map
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody Map<String, String> map) {
        if (userService.sendMsg(map)) {
            return R.success("验证码发送成功");
        }
        return R.error("验证码发送失败");
    }

    /**
     * 用户登录
     * @param session
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R<User> login(HttpSession session,@RequestBody Map<String, String> map) {
        User user = userService.login(session,map);
        if (user !=null) {
            return R.success(user);
        }
        return R.error("登录失败");
    }
    //退出登录
    @PostMapping("/loginout")
    public R<String> logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }
}
